import React, { useState, useEffect } from 'react';

export default function (props) {
  let now = new Date();
  const [date, setDate] = useState(dateFilter(now));
  const [date1, setDate1] = useState(dateFilter1(now));
  const [time, setTime] = useState(timeFilter(now));
  const [week, setWeek] = useState(weekFilter(now));


  function weekFilter(v) {
    let weekArrayList = [ '星期日',   '星期一',   '星期二',   '星期三',   '星期四',   '星期五',   '星期六',  ]
    let index = v.getDay()
    let week = weekArrayList [index]
    return week
  }
  function dateFilter(v) {
    let m = v.getMonth() + 1;
    m = m < 10 ? '0' + m : m;
    let d = v.getDate();
    d = d < 10 ? '0' + d : d;
    return v.getFullYear() + '年' + m + '月' + d + '日';

  }
  function dateFilter1(v) {
    let m = v.getMonth() + 1;
    m = m < 10 ? '0' + m : m;
    let d = v.getDate();
    d = d < 10 ? '0' + d : d;
    return v.getFullYear() + '-' + m + '-' + d;
  }

  function timeFilter(v) {
    let h = v.getHours();
    h = h < 10 ? '0' + h : h;
    let m = v.getMinutes();
    m = m < 10 ? '0' + m : m;
    let s = v.getSeconds();
    s = s < 10 ? '0' + s : s;
    return h + ':' + m + ':' + s;
  }

  let task;
  const start = () => {
    task = setInterval(() => {
      now = new Date();
      setDate(dateFilter(now));
      setDate1(dateFilter1(now));
      setTime(timeFilter(now));
      setWeek(weekFilter(now));
    }, 1000);
  };

  start();

  return {
    date,
    date1,
    time,
    start,
    week,
  };
}
